package game.database.base.shop;

import game.database.common.CommonDB;
import game.util.conf.ConfStatic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.cs.city.bean.base.shop.Recharge;

/**
 * 
 * @author Linzy
 * Create Date: 2010 3 18 11:37:25
 */
public class RechargeDB {
	private static final Logger logger = Logger.getLogger(RechargeDB.class);

	private static String sqlAll = "SELECT id, userID, amount, desp, createDate " +
								"FROM " + ConfStatic.pubDBName + ".recharge ";
	
	private static Recharge loadBean(ResultSet rs){
		Recharge bean = new Recharge();
		try {
			bean.setId(rs.getLong("id"));
			bean.setUserId(rs.getLong("userId"));
			bean.setAmount(rs.getInt("amount"));
			bean.setDesp(rs.getString("desp"));
			bean.setCreateDate(rs.getString("createDate"));
		} catch (Exception e) {
			logger.info("	> ERR in RechargeDB.loadBean() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		}
		return bean;
	}
	
	/**
	 * Get a list of bean from database;
	 * 
	 * @param userId
	 * @return
	 */
	public static List<Recharge> getBeanLst(long userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		Recharge bean = null;
		List<Recharge> beanLst = new ArrayList<Recharge>();
		
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(sqlAll);
			sql.append(" where userId=? ");
			sql.append("order by createDate");

			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setLong(1, userId);
			
			rs = ps.executeQuery();
			while (rs.next()) {
				bean = loadBean(rs);
				if(bean != null){
					beanLst.add(bean);
				}
			}
		} catch (Exception e) {
			logger.info("	> ERR in RechargeDB.getBeanLst() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		} finally {
			CommonDB.destroyConn(conn, null, ps, rs);
			rs = null;
			ps = null;
		}
		return beanLst;
	} // getBeanLst()
	
	/**
	 * Insert a bean to database;
	 * 
	 * @param bean
	 * @return
	 */
	public static boolean insertBean(Recharge bean){
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO " + ConfStatic.pubDBName + ".Recharge(id, userID, amount, desp, createDate)");
		sql.append("VALUES(" + ConfStatic.pubDBName + ".nextval('SEQ_Recharge'), ?, ?, ?, now())");
		try {
			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
			
			ps.setLong(1, bean.getUserId());
			ps.setInt(2, bean.getAmount());
			ps.setString(3, bean.getDesp());
			
			return (ps.executeUpdate() > 0);
		} catch (Exception e) {
			logger.info("	> ERR in RechargeDB.insertBean() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		} finally {
			CommonDB.destroyConn(conn, null, ps, null);
			ps = null;
			conn = null;
		}
		return false;
	} // insertBean()

}
